package com.ams.dao;

import java.sql.SQLException;
import java.util.Date;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.ams.pojo.User;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class UserDao {
	private ComboPooledDataSource ds=new ComboPooledDataSource();
	private QueryRunner qr =new QueryRunner(ds);
	
	public User loginUser(User user) {
		User u=null;
		String sql="select * from user where login_name=? and password=? and type=?";
		try {
			u=qr.query(sql, new BeanHandler<User>(User.class),user.getLogin_name(),user.getPassword(),user.getType());
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return u;
	}
	
	public User selectUser(User user) {
		String sql="select * from user where login_name=?";
		User u=null;
		try {
			u=qr.query(sql, new BeanHandler<User>(User.class),user.getLogin_name());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return u;
	}
	
	public int updateUser(User user) {
		int flag=0;
		String sql="update user set last_login_time=?, logintimes=? where login_name=?";
		Date d=new Date();
		try {
			flag=qr.update(sql,d,user.getLogintimes(),user.getLogin_name());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}
	
}
